package fr.nantes.univ.DAO;

import java.util.List;

import javax.persistence.EntityManager;

import fr.nantes.univ.EMF;
import fr.nantes.univ.EntityBeans.AddressBook;

public class AddressBookDAO {
	
	private static EntityManager instance = EMF.get().createEntityManager();

	public static AddressBook findById(String addressBook) {
		AddressBook ab = instance
				.createQuery("select a from AddressBook a where a.adresse = :adresse", AddressBook.class)
				.setParameter("adresse", addressBook)
				.getSingleResult();
		return ab;
	}
	
	public static List<AddressBook> findAll() {
		List<AddressBook> abs = instance
				.createQuery("select a from AddressBook a", AddressBook.class)
				.getResultList();
		return abs;
	}
	
	public static void insert(AddressBook addressBook) {
		instance.getTransaction().begin();
		instance.persist(addressBook);
		instance.getTransaction().commit();
	}
	
	public static void deleteAll() {
		instance.createQuery("DELETE FROM AddressBook a")
				.executeUpdate();
	}
	
}
